<?php
include_once("./configure/configure.php");						//--> global var
include_once("./admin/configure/admin.config.inc.php");			//-- admin var
include_once("./admin/tools/smarty/libs/Smarty.class.php");		//--> out template

include_once("impl/ImageCustomImpl.php");


include_once("utils/FileUtils.php");				//--> Files
include_once("utils/ImageUtils.php");


if(isset($_POST['Submit'])){
	$width 		= intval($_POST['width']);
	$height		= intval($_POST['height']);
	
	$bgColor	= $_POST['bgColor'];
	$type		= $_POST['type'];
	$fileName	= date("YmdHms");
	
	if(empty($width)){
		$error_message = '请输入你想要的图片宽度。';
	}else if(empty($height)){
		$error_message = '请输入你想要的图片高度。';
	}else{
		// add images
		$imgCusImpl = new ImageCustomImpl(DB_TAG_MM);
		
		$arrImages = array();
		$blacklist = array(".php", ".phtml", ".php3", ".php4");
		
		foreach ($_FILES["image"]["error"] as $key => $error) {
	    	if ($error == UPLOAD_ERR_OK) {
				foreach ($blacklist as $item) {
					if(preg_match("/$item\$/i", $_FILES['image']['name'][$key])) {
					 	$error_message = "仅支持gif、jpg、png图片格式。请把图片转换成jpg格式后再试。";
					}
				}
				
				if(empty($error_message)){
					$imageinfo = getimagesize($_FILES['image']['tmp_name'][$key]); 
		
					if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { 
						$error_message = "仅支持gif、jpg、png图片格式。请把图片转换成jpg格式后再试。";
					}else if($file_size >8000000)
						$error_message = '你上传的图片文件太大，最大图片文件大小限制是8M，当前图片文件大小是('.ceil($file_size/1024/1024).'M)。请把图片转换成jpg格式后再试。';
					else{
						/*-- get ext image file name --*/
						$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
						$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
						
						$arrImage = array();
						$arrImage['name'] 		= $_FILES['image']['name'][$key];
						$arrImage['tmp_name'] 	= $_FILES['image']['tmp_name'][$key];
						$arrImage['file_size']	= filesize($_FILES['image']['tmp_name'][$key]);
						
						$arrImage['width']		= $imageinfo[0];
						$arrImage['height']		= $imageinfo[1];
						
						$arrImage['md5']		= md5(file_get_contents($_FILES['image']['tmp_name'][$key]));
						$arrImage['extName']	= $image_extend_name;
						
						$arrImages[] 			= $arrImage;
					}
				}		
	    	}
	    	
	    	if(!empty($error_message)){
	    		break;
	    	}
		}
		
		foreach ($arrImages as $arrImg) {
			$imgdto = $imgCusImpl->getByMd5($arrImg['md5']);
			if(is_object($imgdto)){		
				$imageName = $imgdto->id . '.' . $imgdto->extName;
				$imgPath = FileUtils::getFullPathByID(IMAGE_PATH_CUSTOM, $imgdto->id, $imageName);
				
				$fileName = $imageName;
//				$error_message = "Image existed, Use it. Image md5 " . $arrImg['md5'];
			}else{	
				$imgdto = new ImageDto();
				
				$imgdto->width			= $arrImg['width'];
				$imgdto->height			= $arrImg['height'];
				$imgdto->extName		= $arrImg['extName'];
				$imgdto->fileSize		= $arrImg['file_size'];
				$imgdto->md5			= $arrImg['md5'];
				
				$imgdto->name 			= '';
				$imgdto->description	= '';
				$imgdto->status			= '0';
				$imgdto->level			= '10';
				$imgdto->isAnimated	= ImageUtils::isAnimatedGif($arrImg['tmp_name']);
				
				$fileName	= $arrImg['tmp_name'];
				
				$imageid = $imgCusImpl->add($imgdto);
				
				if($imageid > 0){									
					$imageName = $imageid . '.' . strtolower($arrImg['extName']);
					$imgPath = FileUtils::getFullPathByID(IMAGE_PATH_CUSTOM, $imageid, $imageName);
					
					if (!move_uploaded_file($arrImg['tmp_name'], $imgPath)) {//-- save image
						$error_message = "保存图片错误，请稍后再试。";
					};					
				}else
					$error_message = "系统繁忙，请稍后再试。";
			}
		}
		
//		echo "error_message: " . $error_message;
		if(empty($error_message)){
			$url 	= HOME_URL . "/admin/pic/thumbnail.php?type=$type&width=$width&height=$height&bgColor=$bgColor&fileName=$fileName";
			
//			echo $url;		
//			exit;
			$file 	= fopen($url, "r");

			header("Content-Type: application/force-download");
			header("Content-Disposition: attachment; filename=$imageName"); 
			
			fpassthru($file);
			exit;
		}
		
	}
	
}


?>
